package com.ims.core.inconfig.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.core.doc.domain.TSalOrder;
import com.ims.core.doc.domain.TScSettlebill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ims.core.inconfig.mapper.TSrmSupplierMapper;
import com.ims.core.inconfig.domain.TSrmSupplier;
import com.ims.core.inconfig.service.ITSrmSupplierService;
import org.springframework.util.CollectionUtils;

/**
 * 企业供应商Service业务层处理
 *
 * @author IMS
 * @date 2024-09-19
 */
@Service
public class TSrmSupplierServiceImpl implements ITSrmSupplierService
{
    @Autowired
    private TSrmSupplierMapper tSrmSupplierMapper;

    /**
     * 查询企业供应商
     *
     * @param id 企业供应商主键
     * @return 企业供应商
     */
    @Override
    public TSrmSupplier selectTSrmSupplierById(Long id)
    {
        return tSrmSupplierMapper.selectTSrmSupplierById(id);
    }

    /**
     * 查询企业供应商列表
     *
     * @param tSrmSupplier 企业供应商
     * @return 企业供应商
     */
    @Override
    public List<TSrmSupplier> selectTSrmSupplierList(TSrmSupplier tSrmSupplier)
    {
        return tSrmSupplierMapper.selectTSrmSupplierList(tSrmSupplier);
    }

    /**
     * 新增企业供应商
     *
     * @param tSrmSupplier 企业供应商
     * @return 结果
     */
    @Override
    public int insertTSrmSupplier(TSrmSupplier tSrmSupplier)
    {
        tSrmSupplier.setCreateTime(DateUtils.getNowDate());
        return tSrmSupplierMapper.insertTSrmSupplier(tSrmSupplier);
    }

    /**
     * 修改企业供应商
     *
     * @param tSrmSupplier 企业供应商
     * @return 结果
     */
    @Override
    public int updateTSrmSupplier(TSrmSupplier tSrmSupplier)
    {
        tSrmSupplier.setUpdateTime(DateUtils.getNowDate());
        return tSrmSupplierMapper.updateTSrmSupplier(tSrmSupplier);
    }

    /**
     * 批量删除企业供应商
     *
     * @param ids 需要删除的企业供应商主键
     * @return 结果
     */
    @Override
    public int deleteTSrmSupplierByIds(Long[] ids)
    {
        return tSrmSupplierMapper.deleteTSrmSupplierByIds(ids);
    }

    /**
     * 删除企业供应商信息
     *
     * @param id 企业供应商主键
     * @return 结果
     */
    @Override
    public int deleteTSrmSupplierById(Long id)
    {
        return tSrmSupplierMapper.deleteTSrmSupplierById(id);
    }

    /**
     *
     * @param tSrmSupplierList
     */
    @Override
    public void syncSupplierByErp(List<TSrmSupplier> tSrmSupplierList) {
        TSrmSupplier tSrmSupplierCondition = new TSrmSupplier();
        for(TSrmSupplier tSrmSupplier : tSrmSupplierList){
            tSrmSupplierCondition.setSupplierCode(tSrmSupplier.getSupplierCode());
            List<TSrmSupplier> orgTSrmSupplierList =  tSrmSupplierMapper.selectTSrmSupplierList(tSrmSupplierCondition);
            if(!CollectionUtils.isEmpty(orgTSrmSupplierList)){
                // 已存在，更新其他信息
                TSrmSupplier orgTSrmSupplier = orgTSrmSupplierList.get(0);
                orgTSrmSupplier.setBankCode(tSrmSupplier.getBankCode());
                orgTSrmSupplier.setContactBy(tSrmSupplier.getContactBy());
                orgTSrmSupplier.setCurrencyCode(tSrmSupplier.getCurrencyCode());
                orgTSrmSupplier.setCurrencyName(tSrmSupplier.getCurrencyName());
                orgTSrmSupplier.setTel(tSrmSupplier.getTel());
                orgTSrmSupplier.setSupplierStatus(tSrmSupplier.getSupplierStatus());
                orgTSrmSupplier.setSupplierStatusName(tSrmSupplier.getSupplierStatusName());
                orgTSrmSupplier.setTermCode(tSrmSupplier.getTermCode());
                orgTSrmSupplier.setTermName(tSrmSupplier.getTermName());
                orgTSrmSupplier.setAddress(tSrmSupplier.getAddress());
                orgTSrmSupplier.setProvinceName(tSrmSupplier.getProvinceName());
                orgTSrmSupplier.setCityName(tSrmSupplier.getCityName());
                orgTSrmSupplier.setSupplierName(tSrmSupplier.getSupplierName());

                orgTSrmSupplier.setUpdateBy("erp");
                orgTSrmSupplier.setUpdateTime(DateUtils.getNowDate());
                tSrmSupplierMapper.updateTSrmSupplier(orgTSrmSupplier);
            } else {
                tSrmSupplierMapper.insertTSrmSupplier(tSrmSupplier);
            }
        }
    }

}
